package com.nbjtjc.safe.api.emg;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgPlanMapper;
import com.nbjtjc.safe.model.emg.EmgPlan;
import com.nbjtjc.safe.service.emg.EmgPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 应急预案
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/emgPlan")
public class EmgPlanController  {

    @Autowired
    private EmgPlanMapper emgPlanMapper;

    @Autowired
    private EmgPlanService emgPlanService;

    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
    @SysLog("删除应急模板")
    public Result delete(int id) {
        Result result=new Result();
        try {
            emgPlanMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    @SysLog("添加应急模板")
    public Result add(EmgPlan emgPlan) {
        Result result=new Result();
        if(emgPlan.getTypeId()==null||emgPlan.getTypeId()==0){
            result.setFalse("type不能为空");
            return result;
        }
        try {
            result=emgPlanService.add(emgPlan);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    @SysLog("更新应急模板")
    public Result update(EmgPlan emgPlan) {
        Result result=new Result();
        if(emgPlan.getTypeId()==null||emgPlan.getTypeId()==0){
            result.setFalse("type不能为空");
            return result;
        }
        try {
            result=emgPlanService.update(emgPlan);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/find",method = {RequestMethod.POST})
    public Result find(@RequestBody EmgPlan emgPlan) {
        Result result=new Result();
        try {
            PageHelper.startPage(emgPlan.getPageCurrent(),emgPlan.getPageSize());
            List<EmgPlan> emgPlans=emgPlanMapper.search(emgPlan);
            result.setData(new PageInfo<>(emgPlans));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

}
